const { app, BrowserWindow, Menu, shell, ipcMain } = require('electron');
const fs = require("fs");
const path = require("path");
config = JSON.parse(fs.readFileSync(path.join(__dirname, './config.json')));
let menu;
let template;
let mainWindow = null;

if (process.env.NODE_ENV === 'production') {
  const sourceMapSupport = require('source-map-support'); // eslint-disable-line
  sourceMapSupport.install();
}

// if (process.env.NODE_ENV === 'development') {
//   require('electron-debug')(); // eslint-disable-line global-require
//   const path = require('path'); // eslint-disable-line
//   const p = path.join(__dirname, '..', 'app', 'node_modules'); // eslint-disable-line
//   require('module').globalPaths.push(p); // eslint-disable-line
// }

function installExtensions() {
  if (process.env.NODE_ENV === 'development') {
    const installer = require('electron-devtools-installer'); // eslint-disable-line global-require   
    const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
    return Promise.all(extensions.map(name => installer.default(installer[name], forceDownload)));
  }

  return Promise.resolve([]);
};

function createWindow() {
  mainWindow = new BrowserWindow({
    show: false,
    width: 1024,
    height: 728
  });

  mainWindow.loadURL(`http://${config.server.host}:${config.server.port}//app.html`);
  let server = require('./server/server') 

  mainWindow.webContents.on('did-finish-load', () => {
    mainWindow.show();
    mainWindow.focus();
  });
  mainWindow.on('closed', () => {
    mainWindow = null;
  });

  if (process.env.NODE_ENV === 'development') {
    mainWindow.openDevTools();
  }
}

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
app.on("browser-window-created", function (e, window) {
  window.setMenu(null);
});

app.on("activate", function () {
  if (mainWindow === null) {
    createWindow();
  }
});


app.on('ready', () =>
  installExtensions()
    .then(() => {
      createWindow();    
    }));
